# Copyright (c) 2005,2006,2007 Dynamic Solutions Inc. (support@dynamic-solutions.com)
#
# This file is part of DataZoomer.
#
# DataZoomer is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# DataZoomer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Shows the datazoomer homepage"""

from dzapp import *
from dzpage import Page
from dzutil import link_to, url_for

class HomeApp(App):

    def _view_(self):
        def list_apps(heading,apps):

            def icon_link(app):
                target = url_for(app=app.name)
                icon   = tag_for('IMG',height=44,src='%s/images/icons/%s.png' % (zoomer.root_url,app.iconname),alt='run the %s app' % (app.name,),border=0)
                link1  = tag_for('A',icon,href=target)
                link2  = tag_for('A',app.title,href=target)
                return '<table border=0><tr><td><center>%s</center></td></tr><tr><td><center>%s</center></td></tr></table>' % (link1,link2)

            debug = zoomer.debug

            if len(apps)==0:
                return ''

            apps_to_show = []
            for app in apps:
                if not app.system_app and not app.name==self.name:
                    apps_to_show.append(app)

            if len(apps_to_show)==0:
                return ''

            apps_to_show.sort(lambda x, y: cmp(x.title.lower(), y.title.lower()))

            icons = [icon_link(app) for app in apps_to_show]

            max = 5
            pct = 100/5

            count = 0
            tablecontent = ['<tr>']
            for icon in icons:
                if count == max:
                    count = 0
                    tablecontent.append('</tr><tr>')
                tablecontent.append('<td width="%d%%" align="center"><center>%s</center></td>' % (pct,icon))
                count += 1
            while count < max:
                tablecontent.append('<td width="%d%%" align="center"><center>&nbsp;</center></td>' % pct)
                count += 1

            tablecontent.append('</tr>')

            content = '!%s\n<table border="0" width="100%%" cellspacing="5">%s</table>\n\n' % (heading,''.join(tablecontent))

            return content

        content  = list_apps('Applications',[app for app in zoomer.user_apps if app.status=='P'])
        content += list_apps('Applications In Testing',[app for app in zoomer.user_apps if app.status=='T'])
        content += list_apps('Applications Under Development',[app for app in zoomer.user_apps if app.status=='D'])
        content += list_apps('Applications On Hold',[app for app in zoomer.user_apps if app.status=='M'])
        content += list_apps('Unregistered Applications',[app for app in zoomer.user_apps if app.status=='U'])
        
        import dzml

        page = Page(dzml.dzml(content))
        page.title = ''
        return page


result = HomeApp()


